// 根据字典类型查询字典数据信息
import http from '@/api/request'
export async function getDicts(dictType) {

	let dicts = [] 
	await httpDicts(dictType).then(res => {
		dicts = formatDicts(res.data)
	})
	return dicts
}
export async function getDict(dictType, dictValue) {

	let dict = ''
	await httpDicts(dictType).then(res => {
		let dicts = formatDicts(res.data, dictValue)
		if(dicts && dicts.length>0){
			dict = dicts[0]
		}
	})
	return dict
}

function httpDicts(dictType) {
	return http({
		url: '/system/dict/data/type/' + dictType,
		method: 'get'
	})
}
export function formatDicts(data, dictValue) {
	let dicts = [];
	if(dictValue){
		data.forEach(dict => {
			if(dictValue==dict.dictValue){
				dicts.push({
					id: dict.dictCode,
					label: dict.dictLabel,
					text: dict.dictLabel,
					value: dict.dictValue,
				})
			}
		})
	}else{
		data.forEach(dict => {
			dicts.push({
				id: dict.dictCode,
				label: dict.dictLabel,
				text: dict.dictLabel,
				value: dict.dictValue,
			})
		})
	}
	
	return dicts
}